<?php
if(!empty($_POST['image'])) {
    //保存base64字符串为图片
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $_POST['image'], $result)){
      $type = $result[2];
      $new_file = "./images/fan-".date('YmdHis').rand(1,1000).".{$type}";
      if (file_put_contents($new_file, base64_decode(str_replace($result[1], '',  $_POST['image'])))){
          echo json_encode(array(
              'success' => true,
              'code' => 200,
              'items' => array(),
              'addon' => array(),
              'detail' => '上传成功',
          ));
      } else {
          echo json_encode(array(
              'success' => false,
              'code' => 200,
              'items' => array(),
              'addon' => array(),
              'detail' => '请上传图片',
          ));
      }
    }
} else if(!empty($_GET['image'])) {
    $dir=dirname(__FILE__)."/images/";//这里输入其它路径
    //PHP遍历文件夹下所有文件
    $handle=opendir($dir.".");
    //定义用于存储文件名的数组
    $array_file = array();
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        $array_file[] = 'images/'.$file; //输出文件名
    }
    }
    closedir($handle);
    echo json_encode(array(
        'success' => true,
        'code' => 200,
        'items' => $array_file,
        'addon' => array(),
        'detail' => '上传成功',
    ));
} else {
    echo json_encode(array(
        'success' => false,
        'code' => 200,
        'items' => array(),
        'addon' => array(),
        'detail' => '访问错误',
    ));
}
